//
//  MainViewController.m
//  Shiyun_Newspaper
//
//  Created by Sylar on 2017/5/25.
//  Copyright © 2017年 ShiYun. All rights reserved.
//

#import "MainViewController.h"
#import "SN_HomeViewController.h"
#import "SN_Question_ViewController.h"
#import "SN_Personal_ViewController.h"
#import "SN_Id_Authentication_ViewController.h"

#import "SY_Activity.h"
#import "SY_Activity_Model.h"


@interface MainViewController () {
    SYNavigationController *homeNav;
    SYNavigationController *questionNav;
    SYNavigationController *personalNav;
    
    SN_HomeViewController *homeVC;
    SN_Question_ViewController *questionVC;
    SN_Personal_ViewController *personalVC;
    
}

@end

@implementation MainViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    //使tabBarController中管理的viqewControllers都符合 UIRectEdgeNone
    if ([UIDevice currentDevice].systemVersion.floatValue >= 7) {
        self.edgesForExtendedLayout = UIRectEdgeNone;
    }
    
    //设置字体
    for (UITabBarItem *item in self.tabBar.items) {
        
        item.imageInsets = UIEdgeInsetsMake(-2, 0, 2, 0);
        
        [item setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                      [UIFont fontWithName:@"Helvetica" size:11.0], NSFontAttributeName, nil]
                            forState:UIControlStateNormal];
        
        [item setTitlePositionAdjustment:UIOffsetMake(-2, -2)];
        
    }
    
    [self setupSubviews];
    
    //[self performSelector:@selector(id_Authentication) withObject:nil afterDelay:0.5];
    
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(schemesAction)
                                                 name:@"URL_Schemes_Notiocation"
                                               object:nil];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}

- (void)schemesAction {
    
    AppDelegate *delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    
    NSDictionary *notiDict = delegate.notiDict;
    
    if (notiDict) {
        
        NSDictionary *dict = [notiDict objectForKey:@"UIApplicationLaunchOptionsRemoteNotificationKey"];
        
        NSString *infoidStr = [NSString stringWithFormat:@"%@",[dict objectForKey:@"infoid"]];
        NSInteger moveto = [[dict objectForKey:@"moveto"] integerValue];
        
        SY_Activity_Model *model = [[SY_Activity_Model alloc] init];
        model.url = [dict objectForKey:@"url"];
        model.infoid = infoidStr;
        model.moveto = moveto;
        model.apptype = 2;
        
        [SY_Activity shareActivity].instructionArr = @[model];
        [[SY_Activity shareActivity] action];
        delegate.notiDict = nil;
    }
    
    
}


- (void)setupSubviews
{
    [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor], NSForegroundColorAttributeName,nil] forState:UIControlStateNormal];
    

    //读报
    homeVC = [[SN_HomeViewController alloc]init];
    homeVC.isRootViewController = YES;
    homeNav = [[SYNavigationController alloc] initWithRootViewController:homeVC];
    
    homeNav.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"读报" image:[UIImage imageWithOriginalName:@"tabbar_Home"] selectedImage:[UIImage imageWithOriginalName:@"tabbar_Home_sel"]];
    [homeNav.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor blackColor]} forState:UIControlStateNormal];
    homeNav.tabBarItem.tag = 0;
    [self unSelectedTapTabBarItems:homeNav.tabBarItem];
    [self selectedTapTabBarItems:homeNav.tabBarItem];
    
    //做题
    questionVC = [[SN_Question_ViewController alloc]init];
    questionVC.isRootViewController = YES;
    questionNav = [[SYNavigationController alloc] initWithRootViewController:questionVC];
    
    questionNav.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"解题" image:[UIImage imageWithOriginalName:@"tabbar_Que"] selectedImage:[UIImage imageWithOriginalName:@"tabbar_Que_sel"]];
    [questionNav.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor blackColor]} forState:UIControlStateNormal];
    questionNav.tabBarItem.tag = 1;
    [self unSelectedTapTabBarItems:questionNav.tabBarItem];
    [self selectedTapTabBarItems:questionNav.tabBarItem];
    
    
    //我的
    personalVC = [[SN_Personal_ViewController alloc]init];
    personalVC.isRootViewController = YES;
    personalNav = [[SYNavigationController alloc] initWithRootViewController:personalVC];
    
    personalNav.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"我的" image:[UIImage imageWithOriginalName:@"tabbar_Par"] selectedImage:[UIImage imageWithOriginalName:@"tabbar_Par_sel"]];
    [personalNav.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor blackColor]} forState:UIControlStateNormal];
    personalNav.tabBarItem.tag = 2;
    [self unSelectedTapTabBarItems:personalNav.tabBarItem];
    [self selectedTapTabBarItems:personalNav.tabBarItem];
    
    self.viewControllers = @[homeNav,questionNav,personalNav];
}


-(void)unSelectedTapTabBarItems:(UITabBarItem *)tabBarItem
{
    [tabBarItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                        [UIFont systemFontOfSize:12], NSFontAttributeName,colorFromHex(@"#3b3b3b"),NSForegroundColorAttributeName,
                                        nil] forState:UIControlStateNormal];
}


-(void)selectedTapTabBarItems:(UITabBarItem *)tabBarItem
{
    [tabBarItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                        [UIFont systemFontOfSize:12],
                                        NSFontAttributeName,DEFAULTCOLOR,NSForegroundColorAttributeName,
                                        nil] forState:UIControlStateSelected];
}

- (void)id_Authentication{
    
    if ([[SYAccountTool ShareAccount].account.ishavecardid isEqualToString:@"0"]) {
        SN_Id_Authentication_ViewController *VC = [[SN_Id_Authentication_ViewController alloc] init];
        
        SYNavigationController *nav = [[SYNavigationController alloc] initWithRootViewController:VC];
        
        [self presentViewController:nav animated:YES completion:nil];
    }
    
}




@end
